﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="开源软件,开源百科" />
    <title>开源百科</title>
    <link rel="Stylesheet" type="text/css" href="css/ospub.css" />
    <link type="text/css" rel="stylesheet" href="css/shCoreDefault.css"/>
    <script type="text/javascript" src="scripts/shCore.js"></script>
	<script type="text/javascript" src="scripts/shBrushJava.js"></script>	
    <script type="text/javascript" src="scripts/kindeditor-min.js"></script>
	<script type="text/javascript" src="scripts/lang/zh_CN.js"></script>
    <script type="text/javascript">
        SyntaxHighlighter.all();
     </script>
     <script type="text/javascript">
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link']
				});
			});
     </script>
</head>
<body>
    <div class="top-nav">
        <div class="bd">
            <div class="login-info">
                <a rel="nofollow" href="#">登录</a> <a href="#">注册</a>
            </div>
            <div class="top-nav-items">
                <ul>
                    <li><a href="#">首页</a> </li>
                    <li><a href="#">开源百科</a></li>
                    <li><a href="#">代码分享</a> </li>
                    <li><a href="#">视频教学</a> </li>
                    <li  class="on"><a href="#">资讯</a> </li>
                </ul>
            </div>
        </div>
    </div>
    <div  class="container">
    <div class="content">
  
    <div class="news-loop"> 
    <div class="header">
    <div class="bd">
     <div class="code-logo">
     代码分享
     </div>
     <div class="code-nav">
     <ul >
     <li>
     <a href="#">最新分享</a>
     </li>
     <li>
     <a href="#">本周热门</a>
     </li>
      <li>
     <a href="#">本月热门</a>
     </li>
     <li>
     <a href="#">年度热门</a>
     </li> 
      <li>
     <a href="#">最新评论</a>
     </li> 
     </ul>
     </div>
    </div>
    </div>
    <div class="code-title">
    <div class="user-header">
    <img  src="http://static.oschina.net/uploads/user/426/853777_50.jpg?t=1357964337000">
    </div>
    <div class="question-title">
    <h1>冒泡排序BubbleSort</h1>
    <div class="publish-info">
     红薯 发布于 2012年09月01日 22时, 54评/5700阅 
     <div class="favorite">
     收藏
     </div>
    </div>
    </div>
    <div class="clear"></div>
    </div>
    <div class="news-content">
    我们知道很多开源软件的无限分类都是采用递归的算法，但是我们知道递归即浪费时间，又浪费空间（内存），
上次我也分享了个我自己原创的无限分类生成树的方法，一热心php高手网友给我提出了宝贵的建议，我测试了一下，这段代码的时间非常之短，
参考：我再次整理了一下，发现数据库查询出数据之后，我们就已经定好了键值，因而实践中，我们一般会在model中查询出格式化成主键值对应数据
的形式，因而我们可以直接用这样的数据，就少了一层循环。代码也非常简洁。 
<div class="code-piece">

<pre class="brush: java; auto-links: false; ">
package com.afreon.util;
import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DesUtil {

	private final static String DES = "DES";

	public static void main(String[] args) throws Exception {
		String data = "123 456";
		String key = "wang!@#$%";
		System.err.println(encrypt(data, key));
		System.err.println(decrypt(encrypt(data, key), key));

	}
	
	/**
	 * Description 根据键值进行加密
	 * @param data 
	 * @param key  加密键byte数组
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String data, String key) throws Exception {
		byte[] bt = encrypt(data.getBytes(), key.getBytes());
		String strs = new BASE64Encoder().encode(bt);
		return strs;
	}

	/**
	 * Description 根据键值进行解密
	 * @param data
	 * @param key  加密键byte数组
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	public static String decrypt(String data, String key) throws IOException,
			Exception {
		if (data == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] buf = decoder.decodeBuffer(data);
		byte[] bt = decrypt(buf,key.getBytes());
		return new String(bt);
	}

	/**
	 * Description 根据键值进行加密
	 * @param data
	 * @param key  加密键byte数组
	 * @return
	 * @throws Exception
	 */
	private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		// 生成一个可信任的随机数源
		SecureRandom sr = new SecureRandom();

		// 从原始密钥数据创建DESKeySpec对象
		DESKeySpec dks = new DESKeySpec(key);

		// 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);

		// Cipher对象实际完成加密操作
		Cipher cipher = Cipher.getInstance(DES);

		// 用密钥初始化Cipher对象
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

		return cipher.doFinal(data);
	}
	
	
	/**
	 * Description 根据键值进行解密
	 * @param data
	 * @param key  加密键byte数组
	 * @return
	 * @throws Exception
	 */
	private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
		// 生成一个可信任的随机数源
		SecureRandom sr = new SecureRandom();

		// 从原始密钥数据创建DESKeySpec对象
		DESKeySpec dks = new DESKeySpec(key);

		// 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);

		// Cipher对象实际完成解密操作
		Cipher cipher = Cipher.getInstance(DES);

		// 用密钥初始化Cipher对象
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

		return cipher.doFinal(data);
	}
}
</pre>
</div>

    </div>
    <div class="code-list">
    <h2><a>相关代码</a></h2>
    <ul>
    <li><span class="lang1">[java]</span><span><a href="#">矩阵加密算法</a></span><span class="pulisher">2月前 by 赵胖子</span></li>
    <li><span class="lang1">[java]</span><span><a href="#">矩阵加密算法</a></span><span class="pulisher">2月前 by 赵胖子</span></li>
    <li><span class="lang1">[java]</span><span><a href="#">矩阵加密算法</a></span><span class="pulisher">2月前 by 赵胖子</span></li>
    <li><span class="lang1">[java]</span><span><a href="#">矩阵加密算法</a></span><span class="pulisher">2月前 by 赵胖子</span></li>
    <li><span class="lang1">[java]</span><span><a href="#">矩阵加密算法</a></span><span class="pulisher">2月前 by 赵胖子</span></li>
    </ul>
    </div>
    <div class="comment-placeholder">
    <h2>
    <a>网友评论</a>
    </h2>

    <ul  class="topic-reply">
    <li id="428412006" class="clearfix comment-item">
    <div class="user-face">
    <a href="http://www.douban.com/people/megan1010/">  
    <img class="pil" alt="AB型小怪兽" src="http://img3.douban.com/icon/u3956060-2.jpg" /> 
    </a>
    </div>
    <div class="reply-doc" style="padding-left:0px;">
    <div class="bg-img-green">
    <h4><a href="http://www.douban.com/people/megan1010/">AB型小怪兽</a>(要放假！！要放假！！)
    <span class="pubtime">2013-01-31 11:19:08</span>
    </h4>   
    </div>  
    <p>看来还是星座控呢……</p>
    </div>
    </li>
   <li id="Li1" class="clearfix comment-item">
    <div class="user-face">
    <a href="http://www.douban.com/people/megan1010/">  
    <img class="pil" alt="AB型小怪兽" src="http://img3.douban.com/icon/u3956060-2.jpg" /> 
    </a>
    </div>
    <div class="reply-doc" style="padding-left:0px;">
    <div class="bg-img-green">
    <h4><a href="http://www.douban.com/people/megan1010/">AB型小怪兽</a>(要放假！！要放假！！)
    <span class="pubtime">2013-01-31 11:19:08</span>
    </h4>   
    </div>  
    <p>看来还是星座控呢……</p>
    </div>
    </li>
    </ul>
    </div>
    <div class="comment-module">
    <textarea name="content" style="width:600px;height:200px;visibility:hidden;">KindEditor</textarea>
    </div>
    </div>
    <div class="siderbar">
    <div class="share">
     与世界分享代码
     <div class="sharecode">
    <a class="sharebutton">
    <span>分享代码</span>
    </a>
    </div>
    </div>

    <div class="yesterday-news">
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;jimlin的其它代码分享</h2>
    <ul>
    <li>如何阻止Chrome劫持RSS文件</li>
    <li>如何阻止Chrome劫持RSS文件</li>
    <li>如何阻止Chrome劫持RSS文件</li>
    <li>如何阻止Chrome劫持RSS文件</li>
    <li>如何阻止Chrome劫持RSS文件</li>
    <li>如何阻止Chrome劫持RSS文件</li>
    </ul>
    </div>

    </div>
    </div>
    </div>
</body>
</html>
